package edu.pku.sei.pgie.ui.preferences;

import org.eclipse.jface.preference.*;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;

import edu.pku.sei.pgie.Activator;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>,
 * we can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class PgiePreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage {

    private FieldEditor outputDirFieldEditor;

    private FieldEditor connectionStringFieldEditor;

    private FieldEditor passwordFieldEditor;

    private FieldEditor userNameFieldEditor;

    public PgiePreferencePage() {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("Pgie Analyzer Preferences");
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors() {
        BooleanFieldEditor isAnalyzeOverride = new BooleanFieldEditor(
                PreferenceConstants.ENABLE_OVERRIDE_ANALYZE,
                "Analyze &Override Relation", getFieldEditorParent());
        addField(isAnalyzeOverride);

        BooleanFieldEditor enableStructureAnalyze = new BooleanFieldEditor(
                PreferenceConstants.ENABLE_STRUCTURE_ANALYZE,
                "Structure Analyze", getFieldEditorParent());
        addField(enableStructureAnalyze);

        BooleanFieldEditor enableMethodAnalyzer = new BooleanFieldEditor(
                PreferenceConstants.ENABLE_METHOD_ANALYZE, "Method Analyze",
                getFieldEditorParent());
        addField(enableMethodAnalyzer);

        addField(new RadioGroupFieldEditor(PreferenceConstants.OUTPUT_FORMAT,
                "Output &Format", 1, new String[][] {
                    {
                        "&Database", "database"
                    }, {
                        "&Xml", "xml"
                    }, {
                        "&Graph", "graph"
                    }
                }, getFieldEditorParent()));

        outputDirFieldEditor = new DirectoryFieldEditor(
                PreferenceConstants.OUPUT_DIR, "Output &Directory:",
                getFieldEditorParent());
        addField(outputDirFieldEditor);

        StringFieldEditor editor = new StringFieldEditor(
                PreferenceConstants.DATABASE_DRIVER_PREFERENCE,
                "Database &Driver", getFieldEditorParent());
        editor.setEnabled(false, getFieldEditorParent());
        addField(editor);

        connectionStringFieldEditor = new StringFieldEditor(
                PreferenceConstants.DATABASE_URL_PREFERENCE,
                "Database &Connectoin String:", getFieldEditorParent());
        addField(connectionStringFieldEditor);

        passwordFieldEditor = new StringFieldEditor(
                PreferenceConstants.DATABASE_USER_PRERFERENCE,
                "Database &User Name:", getFieldEditorParent());
        addField(passwordFieldEditor);

        userNameFieldEditor = new StringFieldEditor(
                PreferenceConstants.DATABASE_PASSWORD_PREFERENCE,
                "Database &Password:", getFieldEditorParent());
        addField(userNameFieldEditor);

    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {}

    public void propertyChange(PropertyChangeEvent event) {
        super.propertyChange(event);
        if (PreferenceConstants.OUTPUT_FORMAT.equals(event.getProperty())) {
            String value = (String) event.getNewValue();
            if (!"database".equals(value)) {
                connectionStringFieldEditor.setEnabled(false,
                        getFieldEditorParent());
                passwordFieldEditor.setEnabled(false, getFieldEditorParent());
                userNameFieldEditor.setEnabled(false, getFieldEditorParent());
                outputDirFieldEditor.setEnabled(true, getFieldEditorParent());
            } else {
                connectionStringFieldEditor.setEnabled(true,
                        getFieldEditorParent());
                passwordFieldEditor.setEnabled(true, getFieldEditorParent());
                userNameFieldEditor.setEnabled(true, getFieldEditorParent());
                outputDirFieldEditor.setEnabled(false, getFieldEditorParent());
            }
        }
    }

}
